<?php

namespace Tiger\Api\Traits;
use Illuminate\Support\Facades\Validator;
use Dingo\Api\Exception\StoreResourceFailedException as StoreResourceFailedException;
use Tiger\Api\Utils\InputWrapper;
trait ValidatorTrait
{
   public $input;

   public $inputParam;


   //校验的属性
    private $validatorAttributes=[
        'house_id'=>'required',
        'community'=>'required',
        'station'=>'required',
        'scheme_notify'=>'',
        'delay'=>'in:0,1',
        'car_number'=>'required',
        'loan_id'=>'required',
        'rate'=>'required',
        'months'=>'required',
        'scheme_type'=>'required|in:0,1,2',
        'money'=>'required',
        'content'=>'required',
        'title'=>'required',
        'bool_field'=>'required|in:position,notify',
        'bool_value'=>'required|in:1,0',
        'passive'=>'required|in:1,0',
        'notify'=>'required|in:1,0',
        'avatar' => 'required|image',
        'comporation'=>'required',
        'order_type'=>'required|in:desc,asc',
        'order'=>'required|in:created_at,real_count',
        'name'=>'required|max:500',//
        'mobile' => 'required|phone',//
        'reset_password'=>'required|min:6',//
        'new_password'=>'required|min:6',//
        'password' => 'required|min:6',//
        'verify_code'=>'required',//
        'pay_type'=>'required|in:apple,aliPay,webChatPay',//
        'page'=>'required|integer',//
        'per_page'=>'required|integer',//
        'id'=>'required|integer',//
        'sn'=>'required',//
        'guard_id'=>'required|integer',//
        'confirm'=>'required|in:ok,cancel',//
       // 'recommend_user'=>'phone|different:mobile',//
        'recommend_user'=>'phone',//
        'img_file' => 'required|image',//
        'field'=>'required|in:name',//
        'value'=>'required',//
        'combo_id'=>'required|integer',//
        'out_trade_no'=>'required',//
        'transaction_id'=>'required|integer',//
        'product_id'=>'required|integer',//
        'start_time'=>'required|date',//
        'end_time'=>'required|date|after:start_time',//
        'notify_time'=>'required|date|before:end_time|after:start_time',//
        'status'=>'required|in:create,open,close',//
        'order_id'=>'integer',//
        'customer_id'=>'required|integer',//
        'log'=>'required|numeric',//
        'lat'=>'required|numeric',//
        'gender'=>'required|in:male,female,other',
        'birthday'=>'required|date',
        'address'=>'',
    ];

     private    $messages = [
         'address.required'=>'请输入地址',
         'birthday.date'=>'出生日期参数不对',
         'birthday.required'=>'请选择出生日期',
         'gender.in'=>'性别输入参数不对',
         'gender.required'=>'请选择性别',
         'end_time.after'=>'截止时间必须晚于开始时间',
         'notify_time.before'=>'提醒时间必须在截止时间之前',
         'notify_time.after'=>'提醒时间必须在开始之间之后',
         'log.required' => '请输入log',
         'log.numeric' => 'log为double',
         'lat.required' => '请输入lat',
         'lat.numeric' => 'lat为double',
         'customer_id.required' => '请输入客户id',
         'customer_id.integer' => '客户id必须数字',
         'order_id.required' => '请输入订单id',
         'order_id.integer' => '订单id必须数字',
         'status.in'=>'status内容不合法',
         'status.required'=>'请输入status',
         'notify_time.required' => '请输入开始时间',
         'notify_time.date'=>'日期格式不对',
         'end_time.required' => '请输入截止时间',
         'end_time.date'=>'日期格式不对',
         'start_time.required' => '请输入开始时间',
         'start_time.date'=>'日期格式不对',
         'product_id.required' => '请输入产品id',
         'product_id.integer' => '产品id必须数字',
         'out_trade_no.required' => '请输入流水号',
         'out_trade_no.integer' => '流水号必须数字',
         'transaction_id.required' => '请输入事务id',
         'transaction_id.integer' => '事务id必须数字',
         'combo_id.required' => '请输入套餐id',
         'combo_id.integer' => '套餐id必须数字',
         'value.required'=>'value不存在',
         'field.required'=>'field不存在',
         'field.in'=>'field内容不合法',
         'img_file.image'=>'必须是图片类型',
         'img_file.required' => '请选择头像',
         'recommend_user.phone'=>'推荐人手机号格式不对',
         'recommend_user.different'=>'推荐人和用户不能相同',
         'confirm.required' => '请确认或取消',
         'confirm.in' => '参数不对',
         'guard_id.required' => '请输入守卫id',
         'guard_id.integer' => '守卫id必须为数字',
         'sn.required' => '请输入唯一识别码',
         'id.required' => '请输入id',
         'id.integer' => 'id必须是整数',
         'page.required' => '请输入页索引',
         'page.integer'=>'必须是整数',
         'per_page.required' => '请输入单页条目数',
         'per_page.integer'=>'必须是整数',
         'pay_type.required' => '请输入支付类型',
         'pay_type.in'=>'支付类型不对',
         'verify_code.required' => '请输入验证码',
         'verify_code.min'=>'验证码必须是四位',
         'verify_code.max'=>'验证码必须是四位',
         'new_password.min'=>'重置密码至少六位密码',
         'new_password.required' => '请输入新密码',
         'password.required' => '请输入密码',
         'password.min'=>'至少六位密码',
         'reset_password.min'=>'重置密码至少六位密码',
         'reset_password.required' => '请输入新密码',
         'mobile.required' => '请输入手机号',
         'mobile.phone'=>'手机号格式不对',
         'name.required' => '请输入用户名',
         'name.max' => '输入用户名过长',
     ];
    /*
     * @param $request 请求
     * @param $request 属性
     * @param $request 规则
     */
    public function Check($request,$parameters,$extRules=null,$extCheck=null)
    {
        if(empty($parameters)) return;

        $attributes=              $parameters;
        $this->input          =   new InputWrapper($request,$parameters);
        $Rules=array();
        //生成需要的规则
       foreach($attributes as $attribute)
        {
            if(array_key_exists($attribute, $this->validatorAttributes))
            {
                $Rules=array_merge($Rules,[$attribute=>$this->validatorAttributes[$attribute]] );
            }

        }
        //替换或者填充定制的的规则
        if(!empty($extRules))
        {
            $Rules=array_merge((array) $Rules,$extRules);
        }

        if(!empty($extCheck)) {
            $needCheck=$request->all();
            $needCheck=array_merge((array) $needCheck,$extCheck);
            $validator = Validator::make($needCheck,$Rules,$this->messages);
        }
        else {
            $validator = Validator::make($request->all(),$Rules,$this->messages);
        }


        if ($validator->fails()) {

            throw new StoreResourceFailedException('参数验证不通过', $validator->errors());
        }
        return true;
    }



}
